// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.IsoWorld;
import zombie.network.packets.INetworkPacket;

public class NetObject implements INetworkPacket {
    public final byte objectTypeNone = 0;
    public final byte objectTypeObject = 1;
    private boolean isProcessed = false;
    private byte objectType = 0;
    private short objectId;
    private int squareX;
    private int squareY;
    private byte squareZ;
    private IsoObject object;

    public void setObject(IsoObject value) {
        this.object = value;
        this.isProcessed = true;
        if (this.object == null) {
            this.objectType = 0;
            this.objectId = 0;
        } else {
            IsoGridSquare isoGridSquare = this.object.square;
            this.objectType = 1;
            this.objectId = (short)isoGridSquare.getObjects().indexOf(this.object);
            this.squareX = isoGridSquare.getX();
            this.squareY = isoGridSquare.getY();
            this.squareZ = (byte)isoGridSquare.getZ();
        }
    }

    public IsoObject getObject() {
        if (!this.isProcessed) {
            if (this.objectType == 0) {
                this.object = null;
            }

            if (this.objectType == 1) {
                IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(this.squareX, this.squareY, this.squareZ);
                if (isoGridSquare == null) {
                    this.object = null;
                } else {
                    this.object = isoGridSquare.getObjects().get(this.objectId);
                }
            }

            this.isProcessed = true;
        }

        return this.object;
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.objectType = b.get();
        if (this.objectType == 1) {
            this.objectId = b.getShort();
            this.squareX = b.getInt();
            this.squareY = b.getInt();
            this.squareZ = b.get();
        }

        this.isProcessed = false;
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putByte(this.objectType);
        if (this.objectType == 1) {
            b.putShort(this.objectId);
            b.putInt(this.squareX);
            b.putInt(this.squareY);
            b.putByte(this.squareZ);
        }
    }

    @Override
    public int getPacketSizeBytes() {
        return this.objectType == 1 ? 12 : 1;
    }

    @Override
    public String getDescription() {
        String string = "";
        switch (this.objectType) {
            case 0:
                string = "None";
                break;
            case 1:
                string = "NetObject";
        }

        return "\n\tNetObject [type="
            + string
            + "("
            + this.objectType
            + ") | id="
            + this.objectId
            + " | pos=("
            + this.squareX
            + ", "
            + this.squareY
            + ", "
            + this.squareZ
            + ") ]";
    }
}
